﻿using System;
using System.Collections.Generic;
using Entity;
using Utils;
using System.Transactions;
using DataBaseConnect.IRepositories;

namespace DataBaseConnect.App
{
    public class RoleAppService : IRoleAppService
    {

        //权限管理仓储接口
        private readonly IRoleRepository _roleRepository;

        /// <summary>
        /// 构造函数 实现依赖注入
        /// </summary>
        /// <param name="userRepository">仓储对象</param>
        public RoleAppService(IRoleRepository roleRepository)
        {
            _roleRepository = roleRepository;
        }

        /// <summary>
        /// 获取全部角色&&查询
        /// </summary>
        /// <param name="RoleName">角色名称</param>
        /// <returns></returns>
        public List<Role> GetRoleList(string RoleName)
        {
            if (!string.IsNullOrEmpty(RoleName))
            {
                return _roleRepository.GetAllList(f => f.RoleName == RoleName);
            }
            else
            {
                return _roleRepository.GetAllList();
            }
        }

        /// <summary>
        /// 获取用户角色
        /// </summary>
        /// <param name="UserId">用户id</param>
        /// <returns></returns>
        public UserRole GetUserRole(int UserId)
        {
            return _roleRepository.GetUserRole(UserId);
        }

        /// <summary>
        /// 新增角色信息
        /// </summary>
        /// <param name="Input">角色信息实体</param>
        /// <returns></returns>
        public ReceiveStatus AddRoleMsg(Role Input)
        {
            ReceiveStatus receiveStatus = new ReceiveStatus();
            try
            {
                _roleRepository.Insert(Input, true);
                receiveStatus.msg = "新增成功";
            }
            catch (Exception ex)
            {
                receiveStatus.code = statuCode.Error;
                receiveStatus.msg = ex.Message;
            }
            return receiveStatus;
        }

        /// <summary>
        /// 设置用户角色
        /// </summary>
        /// <param name="Input">用户角色实体</param>
        /// <returns></returns>
        public ReceiveStatus SetRole(UserRole Input)
        {
            return _roleRepository.SetRole(Input);
        }

        /// <summary>
        /// 编辑角色信息
        /// </summary>
        /// <param name="Input">角色信息实体</param>
        /// <returns></returns>
        public ReceiveStatus EditRoleMsg(Role Input)
        {
            ReceiveStatus receiveStatus = new ReceiveStatus();
            try
            {
                _roleRepository.Update(Input);
                receiveStatus.msg = "编辑成功";
            }
            catch (Exception)
            {
                receiveStatus.code = statuCode.Error;
                receiveStatus.msg = "编辑失败";
            }
            return receiveStatus;
        }

        /// <summary>
        /// 删除角色信息
        /// </summary>
        /// <param name="RoleId">角色id</param>
        /// <returns></returns>
        public ReceiveStatus DelRoleMsg(int RoleId)
        {
            ReceiveStatus receiveStatus = new ReceiveStatus();
            try
            {
                _roleRepository.DelUserRoleMsg(f => f.RoleId == RoleId);
                _roleRepository.DelMenuRoleMsg(f => f.RoleId == RoleId);
                _roleRepository.Delete(f => f.RoleId == RoleId, true);
                receiveStatus.msg = "删除成功";
            }
            catch (Exception ex)
            {
                receiveStatus.code = statuCode.Error;
                receiveStatus.msg = ex.Message;
            }
            return receiveStatus;
        }

        /// <summary>
        /// 根据主键获取角色
        /// </summary>
        /// <param name="RoleId">主键id</param>
        /// <returns></returns>
        public Role GetRoleFirst(int RoleId)
        {
            return _roleRepository.FirstOrDefault(f => f.RoleId == RoleId);
        }
    }
}
